package com.snapp.annot;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;

import com.impetus.annovention.Discoverer;
import com.impetus.annovention.listener.ClassAnnotationDiscoveryListener;
import com.snapp.util.factory.Factory;
import com.snapp.util.object.model.ObjectModel;

public class AnnotationDiscoverer {

	public static void discover(ServletContext context) {
		// get a classpath discoverer instance
        Discoverer discoverer = new SnappClasspathDiscoverer(context);
        discoverer.addAnnotationListener(new ObjectRegistryDiscoverer());
        discoverer.discover();

	}
	
	static class ObjectRegistryDiscoverer implements ClassAnnotationDiscoveryListener {

		private Logger log = Logger.getLogger(this.getClass());

		public String[] supportedAnnotations() {
			log.info("Requested supported annotation types");
			
			return new String[] {SNAppObjectRegistry.class.getName()};
		}
		
		public void discovered(String className, String annotation) {
			log.info("Discovered Class(" + className + ") with Annotation(" + annotation + ")");
			
			try {
				Factory.registerObject((ObjectModel)Class.forName(className).cast(ObjectModel.class));
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
